﻿using System.Data;
using System.Data.SqlClient;

namespace _Ado.NET调用存储并使用OutPut
{
    public class SqlUtil
    {
        public static string ExecuteNonQueryOutPut(string connString, string sqlProcedureName, string outSqlParameterKey, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigUtil.DefaultConnString))
            {
                string outString = string.Empty;
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = sqlProcedureName;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    cmd.Parameters.Add(outSqlParameterKey, SqlDbType.NVarChar, 1024).Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    outString = cmd.Parameters[outSqlParameterKey].Value.ToString();
                }
                return outString;
            }
        }

        public static string ExecuteNonQueryOutPutV2(string connString, string sqlProcedureName, string outParameterKey, params SqlParameter[] para)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigUtil.DefaultConnString))
            {
                string outString = string.Empty;
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = sqlProcedureName;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    cmd.Parameters.AddRange(para);
                    cmd.ExecuteNonQuery();
                    outString = cmd.Parameters[outParameterKey].Value.ToString();
                }
                return outString;
            }
        }
    }
}